﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using DataAccess;
using Model;

namespace CheckList
{
    public partial class HazopTeamPage2 : UserControl{
        public HazopTeamPage2()
        {
            InitializeComponent();   
            _bindHazopTeam.DataSource = Program.Workspace.CheckListProject.HazardTeamList;
            dgvHazopTeam.DataSource = _bindHazopTeam;
            AddColumns();
        }

        private void AddColumns(){
            for (int i = 0; i < 50; i++){
                MDataGridViewCalendarColumn calendarColumn = new MDataGridViewCalendarColumn();
                calendarColumn.HeaderText = "第"+(i+1)+"天";
                calendarColumn.Name = i.ToString();
                dgvHazopTeam.Columns.Add(calendarColumn);
            }
            for (int i = 0; i < Program.Workspace.CheckListProject.HazardTeamList.Count; i++)
            {
                for (int j = 0; j < 50; j++){
                    if (Program.Workspace.CheckListProject.HazardTeamList[i].DateList[j].Equals(""))
                         break;
                    dgvHazopTeam.Rows[i].Cells[j.ToString()].Value = Program.Workspace.CheckListProject.HazardTeamList[i].DateList[j];
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e){
            dgvHazopTeam.Enabled = true;
            for (int i = 0; i < Program.Workspace.CheckListProject.HazardTeamList.Count; i++)
            {
                for (int j = 0; j < 50; j++)
                {
                    if (Program.Workspace.CheckListProject.HazardTeamList[i].DateList[j].Equals(""))
                        break;
                    dgvHazopTeam.Rows[i].Cells[j.ToString()].Value = Program.Workspace.CheckListProject.HazardTeamList[i].DateList[j];
                }
            }
        }

        private List<string> GetTitleList(){
            List<string> titleList = new List<string>();
            XmlNodeList nodeList = XMLHelper.GetXmlNodeListByXpath("./Config/HazopTeam.xml","/HazopTeam/Title");
            foreach (XmlNode node in nodeList){
                titleList.Add(node.InnerText);
            }
            return titleList;
        }

        private void BindTitleColumnToolTips(){
            List<string> titleList = GetTitleList();
            string toolTips = string.Empty;
            foreach (string s in titleList){
                toolTips += (s + '\n');
            }
            if (dgvHazopTeam.CurrentRow == null){
                return;
            }
           // dgvHazopTeam.Columns[3].ToolTipText = toolTips;
            dgvHazopTeam.CurrentRow.Cells[3].ToolTipText = toolTips;
        }

        private void dgvHazopTeam_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e){
           BindTitleColumnToolTips();
        }

        private void dgvHazopTeam_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvHazopTeam.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvHazopTeam.RowHeadersDefaultCellStyle.Font, rectangle,
                dgvHazopTeam.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dgvHazopTeam_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e == null || e.Control == null || e.Control.GetType() != typeof(DataGridViewTextBoxEditingControl))
                return;
            DataGridViewTextBoxEditingControl te = (DataGridViewTextBoxEditingControl)e.Control;
            te.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //建议追加 
            te.AutoCompleteSource = AutoCompleteSource.CustomSource;
            if (dgvHazopTeam.CurrentCell.OwningColumn.Name.Equals("colTitle"))
            {
                List<string> remindTitleList = new List<string>();
                remindTitleList = GetTitleList();
                te.AutoCompleteCustomSource.AddRange(GetRemindTitleList(remindTitleList));
                //  GetRemindReasonList(remindReasonList);
            }
        }

        private string[] GetRemindTitleList(List<string> reasonList)
        {
            string[] result = new string[reasonList.Count];
            for (int i = 0; i < reasonList.Count; i++)
            {
                result[i] = reasonList[i];
            }
            return result;
        }

        public void btnSave_Click(object sender, EventArgs e){
            for (int i = 0; i < dgvHazopTeam.Rows.Count; i++ )
            {   
                for (int j = 0; j < 50; j++){
                    if (dgvHazopTeam.Rows[i].Cells[j].Value == null)
                    {
                        break;
                    }
                    string tmp = Convert.ToDateTime(dgvHazopTeam.Rows[i].Cells[j.ToString()].Value).ToShortDateString();
                    if (Program.Workspace.CheckListProject.HazardTeamList[i].DateList[j].Equals(""))
                    {
                        Program.Workspace.CheckListProject.HazardTeamList[i].DateList[j] = tmp;
                    }
                }
            }
            Program.Workspace.SaveProject(MainForm.mainForm.SaveFilePath);
            dgvHazopTeam.Enabled = false;
            MessageBox.Show("Hazop小组保存成功!");
        }

        public void SaveWithoutRemider(){
            for (int i = 0; i < dgvHazopTeam.Rows.Count; i++)
            {
                for (int j = 0; j < 50; j++)
                {
                    if (dgvHazopTeam.Rows[i].Cells[j].Value == null)
                    {
                        break;
                    }
                    string tmp = Convert.ToDateTime(dgvHazopTeam.Rows[i].Cells[j.ToString()].Value).ToShortDateString();
                    if (Program.Workspace.CheckListProject.HazardTeamList[i].DateList[j].Equals(""))
                    {
                        Program.Workspace.CheckListProject.HazardTeamList[i].DateList[j] = tmp;
                    }
                }
            }
            dgvHazopTeam.Enabled = false;
        }

        public DataGridView DgvHazopTeam{
            get { return dgvHazopTeam; }
            set { dgvHazopTeam = value; }
        }

        private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(!btnEdit.Enabled){
                return;
            }
            HazopTeam item = new HazopTeam();
            for (int i = 0; i < 50; i++){
                item.DateList.Add("");
            }
            _bindHazopTeam.Insert(dgvHazopTeam.Rows.Count,item);
            dgvHazopTeam.Rows[dgvHazopTeam.Rows.Count - 1].Selected = true;
            dgvHazopTeam.CurrentCell = dgvHazopTeam[1, dgvHazopTeam.Rows.Count - 1];
        }


    }
}
